import NotFound from '@/components/404/NotFound'
import Login from '@/views/login/Index'
import { Navigate, useRoutes } from 'react-router-dom'
import { RouteObject } from './interface'
//files返回一个函数
const files = require.context('./modules', false, /\.ts$/, 'sync')
const routerModules: RouteObject[] = []

files.keys().forEach(key => {
  routerModules.push(...files(key).default)
})
export const rootRouter: RouteObject[] = [
  {
    path: '/login',
    element: <Login />,
    meta: {
      title: '登录',
    },
  },
  { path: '/404', element: <NotFound /> },
  ...routerModules,
  {
    path: '*',
    element: <Navigate to="/404" replace />,
  },
]

//通过files.keys()遍历获取每个id，然后调用files(id),把遍历获取的id传入这个函数，最后返回一个模块，通过.default获取

export default function Router() {
  const routes = useRoutes(rootRouter)

  return routes
}
